/*******************************************************************************
 * Copyright 2009 DCSpectrometer - http://code.google.com/p/dcspectrometer 
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *******************************************************************************/
package com.dcspectrometer;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;

/**
 * Detects diagonal edges
 */
public class EdgeDetector extends Detector {
  private double minimumThreshold;
  private double minimumIluminationDifference;

  /**
   * Constructor
   * @param minimumThreshold minimum average value to be evaluated.
   * @param minimumIluminationDifference minimum lighting difference (in Stops) 
   */
  public EdgeDetector(double minimumThreshold, double minimumIluminationDifference) {
    this.minimumThreshold = minimumThreshold;
    this.minimumIluminationDifference = minimumIluminationDifference;
  }

  public boolean detectFeature(BufferedImage image, int x, int y, int width, int height) {
    int halfWidth = width / 2;
    int halfHeight = height / 2;
    /**
     * Split image into 4 sectors: Q1 (Top Left), Q2 (Top Right), Q3 (Lower Left), Q4 (Lower Right)
     */ 
    Raster q1Raster = image.getData(new Rectangle(x, y, halfWidth, halfHeight));
    RasterTileStatistics q1 = new RasterTileStatistics(q1Raster);
    Raster q2Raster = image.getData(new Rectangle(x + halfWidth, y, width - halfWidth, halfHeight));
    RasterTileStatistics q2 = new RasterTileStatistics(q2Raster);
    Raster q3Raster = image
        .getData(new Rectangle(x, y + halfHeight, halfWidth, height - halfHeight));
    RasterTileStatistics q3 = new RasterTileStatistics(q3Raster);
    Raster q4Raster = image.getData(new Rectangle(x + halfWidth, y + halfHeight, width - halfWidth,
        height - halfHeight));
    RasterTileStatistics q4 = new RasterTileStatistics(q4Raster);

    double[] q1Mean = q1.getMean();
    double[] q2Mean = q2.getMean();
    double[] q3Mean = q3.getMean();
    double[] q4Mean = q4.getMean();

    double threshold = Math.pow(2, minimumIluminationDifference);

    for (int i = 0; i < q1Raster.getNumBands(); i++) {
      if (q1Mean[i] > minimumThreshold && q4Mean[i] > minimumThreshold) {
        // Q1/Q4
        if (((q1Mean[i] / q4Mean[i]) > threshold) || ((q4Mean[i] / q1Mean[i]) > threshold)) {
          return true;
        }
      }

      // Q2/Q3
      if (q2Mean[i] > minimumThreshold && q3Mean[i] > minimumThreshold) {
        if (((q2Mean[i] / q3Mean[i]) > threshold) || ((q3Mean[i] / q2Mean[i]) > threshold)) {
          return true;
        }
      }
    }

    return false;
  }
}
